package invs.invs300both;

import android.app.Activity;
import android.app.AlertDialog;
import android.bluetooth.BluetoothAdapter;
import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.invs.BtReaderClient;
import com.invs.IClientCallBack;
import com.invs.InvsDes3;
import com.invs.InvsIdCard;
import com.invs.InvsUtil;
import com.invs.invswlt;

import java.io.ByteArrayOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


public class NewBltDemo extends Activity implements OnClickListener, IClientCallBack {


    public static final int KEY_SOUND_A11 = 11;
    public static final int KEY_SOUND_A12 = 12;
    public static final int KEY_SOUND_A13 = 13;
    public static final int KEY_SOUND_A14 = 14;
    public static final int KEY_SOUND_A15 = 15;
    public static final int KEY_SOUND_A16 = 16;
    public static final int KEY_SOUND_A17 = 17;
    public static final int KEY_SOUND_A18 = 18;
    public static final int KEY_SOUND_A19 = 19;
    public static final int KEY_SOUND_A20 = 20;
    public static final int KEY_SOUND_A97 = 97;
    public static final int KEY_SOUND_A98 = 98;
    public static final int KEY_SOUND_A99 = 99;
    public static final int KEY_SOUND_A01 = 1;
    public static final int KEY_SOUND_A02 = 2;
    public static final int KEY_SOUND_A03 = 3;
    public static final int KEY_SOUND_A04 = 4;
    public static final int KEY_SOUND_A05 = 5;
    public static final int KEY_SOUND_A06 = 6;
    static int id[] = {R.id.button1, R.id.button2, R.id.button3, R.id.button4, R.id.button5, R.id.button6, R.id.button7, R.id.button8, R.id.button9, R.id.button10};
    private BtReaderClient mClient = null;
    InvsIdCard mCard;
    SoundPool mSoundPool;
    private static HashMap<Integer, Integer> soundPoolMap;

    @Override
    protected void onDestroy() {
        mClient.disconnectBt();
        super.onDestroy();
        System.exit(0);
    }

    private byte[] Bmp2Jpg(byte[] bmpData) {
        Bitmap bm = BitmapFactory.decodeByteArray(bmpData, 0, bmpData.length);
        //bm = Alpha0(bm);
        ByteArrayOutputStream jpgData = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.JPEG, 60, jpgData);
        return jpgData.toByteArray();
    }

    //外国人
    private void displayView_wgr(InvsIdCard invsIdCard) {
        ContentValues value = new ContentValues();

        TextView v = (TextView) findViewById(R.id.textView1);
        v.setText("英文名 " + invsIdCard.address + "\r\n中文名 " + invsIdCard.name);
        value.put("name", invsIdCard.name);

        v = (TextView) findViewById(R.id.textView2);
        String szTmp = invsIdCard.sex;
        if (szTmp.compareToIgnoreCase("女") == 0) {
            szTmp = "女/F";
        } else {
            szTmp = "男/M";
        }
        v.setText("性别  " + szTmp);
        value.put("sex", invsIdCard.sex);

        v = (TextView) findViewById(R.id.textView3);
        szTmp = invsIdCard.birth;
        szTmp = szTmp.substring(0, 4) + "."
                + szTmp.substring(4, 6) + "."
                + szTmp.substring(6, 8);

        v.setText("出生  " + szTmp);
        value.put("birth", invsIdCard.birth);

        v = (TextView) findViewById(R.id.textView4);
        szTmp = invsIdCard.nation;
        String[] s = GuoJiPair.GetGuoJi(szTmp);
        if (s != null) {
            v = (TextView) findViewById(R.id.textView4);
            v.setText("国籍 " + s[2] + "/" + s[1]);
        }
        value.put("nation", invsIdCard.nation);
        value.put("addr", invsIdCard.address);

        v = (TextView) findViewById(R.id.textView5);
        szTmp = invsIdCard.start;
        szTmp = szTmp.substring(0, 4) + "." +
                szTmp.substring(4, 6) + "." +
                szTmp.substring(6, 8) + "-";
        if (invsIdCard.end.length() == 8) {
            szTmp = szTmp + invsIdCard.end.substring(0, 4) + "." +
                    invsIdCard.end.substring(4, 6) + "." +
                    invsIdCard.end.substring(6, 8);
        } else {
            szTmp = szTmp + invsIdCard.end;
        }

        szTmp = invsIdCard.start;
        szTmp = szTmp.substring(0, 4) + "." + szTmp.substring(4, 6) + "." + szTmp.substring(6, 8);
        value.put("begin", szTmp);

        String szTmp1 = invsIdCard.end;
        if (szTmp1.length() >= 8) {
            szTmp1 = szTmp1.substring(0, 4) + "." + szTmp1.substring(4, 6) + "." + szTmp1.substring(6, 8);
            value.put("end", szTmp1);
        } else {
            value.put("end", szTmp1);
        }
        szTmp += "-" + szTmp1;
        v.setText("有效期限  " + szTmp);

        v = (TextView) findViewById(R.id.textView6);
        v.setText("签发机关  " + "公安部/Ministry of Public Security");
        value.put("police", invsIdCard.police);

        v = (TextView) findViewById(R.id.textView7);
        v.setText("身份证号 " + invsIdCard.idNo);
        value.put("idnum", invsIdCard.idNo);

        value.put("finger", "");
        value.put("appaddr", "");

        v = (TextView) findViewById(R.id.textView11);

        if (invsIdCard.id == null) {
            v.setText("读卡成功");
        } else {
            v.setText("读卡成功,卡id:" + byte2hex(invsIdCard.id));
        }

        byte[] szBmp = invswlt.Wlt2Bmp(invsIdCard.wlt);

        if ((szBmp != null) && (szBmp.length == 38862)) {
            Bitmap bmp = BitmapFactory.decodeByteArray(szBmp, 0, szBmp.length);
            ImageView iv = (ImageView) findViewById(R.id.imageView1);
            iv.setImageBitmap(bmp);

            byte[] JPG = Bmp2Jpg(szBmp);
            value.put("jpg", JPG);

        } else {
            ImageView mImageView = (ImageView) findViewById(R.id.imageView1);
            mImageView.setImageDrawable(getResources().getDrawable(R.drawable.tmp));
        }

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        value.put("time", format.format(new Date()).toString());

        //crdbh.insert(value);
    }

    void displayView(InvsIdCard invsIdCard) {
        byte c = invsIdCard.address.toLowerCase().getBytes()[0];
        if ((c >= "a".getBytes()[0] && c <= "z".getBytes()[0])) {
            displayView_wgr(invsIdCard);
            Log.e(TAG,"c >= \"a\".getBytes()[0] &");
            return;
        }else {
            Log.e(TAG,"c >cccccccccccc");
        }

        Log.e(TAG,invsIdCard.address.toLowerCase());
        Log.e(TAG,invsIdCard.address);
        ContentValues value = new ContentValues();

        TextView v = (TextView) findViewById(R.id.textView1);
        v.setText("姓名  " + invsIdCard.name);
        value.put("name", invsIdCard.name);

        v = (TextView) findViewById(R.id.textView2);
        v.setText("性别  " + invsIdCard.sex);
        value.put("sex", invsIdCard.sex);

        v = (TextView) findViewById(R.id.textView3);
        if (invsIdCard.nation.length() == 0) {
            v.setText("通行证号码 " + invsIdCard.txzNo + "   签发次数 " + invsIdCard.txzCnt);
            value.put("nation", invsIdCard.nation);
        } else {
            v.setText("民族  " + invsIdCard.nation);
            value.put("nation", invsIdCard.nation);
        }

        v = (TextView) findViewById(R.id.textView4);
        String szTmp = invsIdCard.birth;
        szTmp = szTmp.substring(0, 4) + "年"
                + szTmp.substring(4, 6) + "月"
                + szTmp.substring(6, 8) + "日";

        v.setText("出生  " + szTmp);
        value.put("birth", invsIdCard.birth);

        v = (TextView) findViewById(R.id.textView5);
        v.setText("住址  " + invsIdCard.address);
        value.put("addr", invsIdCard.address);


        v = (TextView) findViewById(R.id.textView6);
        v.setText("身份证号 " + invsIdCard.idNo);
        value.put("idnum", invsIdCard.idNo);

        v = (TextView) findViewById(R.id.textView7);
        v.setText("签发机关  " + invsIdCard.police);
        value.put("police", invsIdCard.police);

        v = (TextView) findViewById(R.id.textView8);

        szTmp = invsIdCard.start;
        szTmp = szTmp.substring(0, 4) + "." +
                szTmp.substring(4, 6) + "." +
                szTmp.substring(6, 8) + "-";
        if (invsIdCard.end.length() == 8) {
            szTmp = szTmp + invsIdCard.end.substring(0, 4) + "." +
                    invsIdCard.end.substring(4, 6) + "." +
                    invsIdCard.end.substring(6, 8);
        } else {
            szTmp = szTmp + invsIdCard.end;
        }
        szTmp = invsIdCard.start;
        szTmp = szTmp.substring(0, 4) + "." + szTmp.substring(4, 6) + "." + szTmp.substring(6, 8);
        value.put("begin", szTmp);

        String szTmp1 = invsIdCard.end;
        if (szTmp1.length() >= 8) {
            szTmp1 = szTmp1.substring(0, 4) + "." + szTmp1.substring(4, 6) + "." + szTmp1.substring(6, 8);
            value.put("end", szTmp1);
        } else {
            value.put("end", szTmp1);
        }

        szTmp += "-" + szTmp1;
        value.put("finger", "");
        value.put("appaddr", "");

        v.setText("有效期限  " + szTmp);

        v = (TextView) findViewById(R.id.textView11);
        if (invsIdCard.id == null) {
            v.setText("读卡成功");
        } else {
            v.setText("读卡成功,卡id:" + byte2hex(invsIdCard.id));
        }

        v = (TextView) findViewById(R.id.textView12);
        if (invsIdCard.finger == null || invsIdCard.finger.length != 1024) {
            //v.setText("无指纹信息");
            switchBtn(new int[]{R.id.button1, R.id.button6}, false);
        } else {
            //v.setText("采集指纹1:" + GetFingerPos(invsIdCard.finger[5]) + "\r\n采集指纹2:" + GetFingerPos(invsIdCard.finger[517]));
            switchBtn(new int[]{R.id.button1}, false);
        }

        byte[] szBmp = invswlt.Wlt2Bmp(invsIdCard.wlt);

        if ((szBmp != null) && (szBmp.length == 38862)) {
            Bitmap bmp = BitmapFactory.decodeByteArray(szBmp, 0, szBmp.length);
            ImageView iv = (ImageView) findViewById(R.id.imageView1);
            iv.setImageBitmap(bmp);

            byte[] JPG = Bmp2Jpg(szBmp);
            value.put("jpg", JPG);

        } else {
            ImageView mImageView = (ImageView) findViewById(R.id.imageView1);
            mImageView.setImageDrawable(getResources().getDrawable(R.drawable.tmp));
        }

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        value.put("time", format.format(new Date()).toString());

        //crdbh.insert(value);
    }

    public static String byte2hex(byte[] data) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < data.length; i++) {
            String temp = Integer.toHexString(((int) data[i]) & 0xFF);
            for (int t = temp.length(); t < 2; t++) {
                sb.append("0");
            }
            sb.append(temp);
        }
        return sb.toString();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
        if (btAdapter == null) {
            finish();
            return;
        }

        if (!btAdapter.isEnabled()) {
            btAdapter.enable();
        }

        initBtn();
        initView("");

        mClient = new BtReaderClient(this);
        mClient.setCallBack(this);

        mSoundPool = new SoundPool(1, AudioManager.STREAM_ALARM, 0);
        mSoundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
            @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {

            }
        });
        soundPoolMap = new HashMap<Integer, Integer>();
        soundPoolMap.put(KEY_SOUND_A11, mSoundPool.load(this, R.raw.a11, 1));
        soundPoolMap.put(KEY_SOUND_A12, mSoundPool.load(this, R.raw.a12, 1));
        soundPoolMap.put(KEY_SOUND_A13, mSoundPool.load(this, R.raw.a13, 1));
        soundPoolMap.put(KEY_SOUND_A14, mSoundPool.load(this, R.raw.a14, 1));
        soundPoolMap.put(KEY_SOUND_A15, mSoundPool.load(this, R.raw.a15, 1));
        soundPoolMap.put(KEY_SOUND_A16, mSoundPool.load(this, R.raw.a16, 1));
        soundPoolMap.put(KEY_SOUND_A17, mSoundPool.load(this, R.raw.a17, 1));
        soundPoolMap.put(KEY_SOUND_A18, mSoundPool.load(this, R.raw.a18, 1));
        soundPoolMap.put(KEY_SOUND_A19, mSoundPool.load(this, R.raw.a19, 1));
        soundPoolMap.put(KEY_SOUND_A20, mSoundPool.load(this, R.raw.a20, 1));
        soundPoolMap.put(KEY_SOUND_A97, mSoundPool.load(this, R.raw.a97, 1));
        soundPoolMap.put(KEY_SOUND_A98, mSoundPool.load(this, R.raw.a98, 1));
        soundPoolMap.put(KEY_SOUND_A99, mSoundPool.load(this, R.raw.a99, 1));
        soundPoolMap.put(KEY_SOUND_A01, mSoundPool.load(this, R.raw.a01, 1));
        soundPoolMap.put(KEY_SOUND_A02, mSoundPool.load(this, R.raw.a02, 1));
        soundPoolMap.put(KEY_SOUND_A03, mSoundPool.load(this, R.raw.a03, 1));
        soundPoolMap.put(KEY_SOUND_A04, mSoundPool.load(this, R.raw.a04, 1));
        soundPoolMap.put(KEY_SOUND_A05, mSoundPool.load(this, R.raw.a05, 1));
        soundPoolMap.put(KEY_SOUND_A06, mSoundPool.load(this, R.raw.a06, 1));

    }

    private static final String TAG = "XWZN";

    private String getAddr() {
        SharedPreferences sp = getSharedPreferences("BindDevice", Activity.MODE_PRIVATE);
        Log.e(TAG, sp.getString("Address", ""));
        return sp.getString("Address", "");
    }

    void testMap(Map map) throws Exception {
        Integer i = (Integer) map.get("resultFlag");
        String xml = "";
        int ii = i;
        String mac = (String) map.get("mac");
        if (ii != 0) {
            initView("读卡失败:");
            xml = "mac:" + mac + "\r\n" + "errorMsg:" + map.get("errorMsg");
            new AlertDialog.Builder(this)
                    .setMessage(xml)
                    .setPositiveButton("Ok", null)
                    .show();
            return;
        }
        String data = (String) map.get("resultContent");
        InvsDes3 des3 = new InvsDes3();

        if (true) {
            byte[] des = null;
            try {
                String key_str = "303132333435363738394142434445463031323334353637";
                des = des3.des3DecodeCBC(key_str, null, InvsBase64.decode(data));
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            xml = "mac:" + mac + "\r\n" + "errorMsg:" + map.get("errorMsg") + "\r\n" + new String(des, "UTF8");
        } else {

        }
        new AlertDialog.Builder(this)
                .setMessage(xml)
                .setPositiveButton("Ok", null)
                .show();
    }


    public void onClick(View v) {
        int iSound = 0;
        byte finger[] = new byte[1];

        TextView v11 = (TextView) findViewById(R.id.textView11);
        switch (v.getId()) {
            case R.id.button1:
                switchBtn(new int[]{}, true);
                initView("正在连接设备");

                if (!mClient.connectBt(getAddr())) {
                    initView("连接失败");
                    switchBtn(new int[]{R.id.button1, R.id.button4, R.id.button5}, true);
                } else {
                    switchBtn(new int[]{R.id.button1, R.id.button4, R.id.button6, R.id.button7}, false);
                    initView("连接成功");
                }
                break;


            case R.id.button2: {
                switchBtn(new int[]{}, true);
                initView("正在读卡");

                Log.e(TAG, "bDes   shang ");
                InvsIdCard card = mClient.readCard();//不带卡id
                if (card != null) {
                    displayView(card);
                    mCard = card;
                    if (mCard.finger == null || mCard.finger.length != 1024) {
                        switchBtn(new int[]{R.id.button1, R.id.button4, R.id.button6, R.id.button7}, false);
                        return;
                    }
                    switchBtn(new int[]{R.id.button1, R.id.button4}, false);
                    mSoundPool.play(soundPoolMap.get(KEY_SOUND_A01), 1, 1, 0, 0, 1);
                } else {
                    initView("读卡失败");
                    switchBtn(new int[]{R.id.button2, R.id.button4, R.id.button5}, true);
                }


            }
            break;
            case R.id.button3:
                switchBtn(new int[]{}, true);
                mClient.disconnectBt();
                switchBtn(new int[]{R.id.button1, R.id.button4, R.id.button5}, true);
                break;
            case R.id.button4:
                Intent intent = new Intent(NewBltDemo.this, FindBtActivity.class);
                NewBltDemo.this.startActivity(intent);
                break;
            case R.id.button5:
                Intent i = new Intent(this, CardHisActivity.class);
                //Intent i = new Intent(this,FindBtActivity.class);
                NewBltDemo.this.startActivity(i);
                break;
            case R.id.button8:
                byte[] BCard = mClient.getCardId();
                if (BCard == null) {
                    v11.setText("读卡id失败");
                } else {
                    v11.setText("读卡id成功,id:" + InvsUtil.bytesToHexString(BCard, 0, 8));
                }


                break;
            case R.id.button9:
                byte[] ACard = mClient.getACardId();
                if (ACard == null) {
                    v11.setText("读卡id失败");
                } else {
                    v11.setText("读卡id成功,id:" + InvsUtil.bytesToHexString(ACard, 0, 8));
                }
                break;
            case R.id.button10:
                String s = mClient.getVer();
                if (s == null) {
                    v11.setText("读卡硬件版本失败");
                } else {
                    v11.setText("读卡硬件版本成功,版本:\r\n" + s);
                }
                break;
            case R.id.button6:
                iSound = mCard.finger[5];

                mSoundPool.play(soundPoolMap.get(iSound), 1, 1, 0, 0, 1);

                if (!mClient.openFinger()) {
                    TextView v1 = (TextView) findViewById(R.id.textView11);
                    v1.setText("指纹采集器异常");
                    mClient.closeFinger();
                    mSoundPool.play(soundPoolMap.get(KEY_SOUND_A05), 1, 1, 0, 0, 1);
                    break;
                }

                SystemClock.sleep(1000);

                if (!mClient.collectFinger()) {
                    TextView v1 = (TextView) findViewById(R.id.textView11);
                    v1.setText("指纹采集失败");
                    mClient.closeFinger();
                    mSoundPool.play(soundPoolMap.get(KEY_SOUND_A05), 1, 1, 0, 0, 1);
                    break;
                }

                SystemClock.sleep(1000);
                mSoundPool.play(soundPoolMap.get(KEY_SOUND_A06), 1, 1, 0, 0, 1);

                finger[0] = mCard.finger[5];
                //System.arraycopy(mCard.finger, 0, finger, 0, finger.length);
                //String ss = InvsUtil.bytesToHexString(finger, 0, finger.length);
                if (!mClient.downFinger(null)) {
                    TextView v1 = (TextView) findViewById(R.id.textView11);
                    v1.setText("指纹下载失败");
                    mClient.closeFinger();
                    SystemClock.sleep(1200);
                    mSoundPool.play(soundPoolMap.get(KEY_SOUND_A05), 1, 1, 0, 0, 1);
                    break;
                }

                //SystemClock.sleep(3000);
                if (!mClient.matchFinger()) {
                    TextView v1 = (TextView) findViewById(R.id.textView11);
                    v1.setText("指纹比对失败");
                    mClient.closeFinger();
                    SystemClock.sleep(1200);
                    mSoundPool.play(soundPoolMap.get(KEY_SOUND_A05), 1, 1, 0, 0, 1);
                } else {
                    TextView v1 = (TextView) findViewById(R.id.textView11);
                    v1.setText("指纹比对成功");
                    mClient.closeFinger();
                    SystemClock.sleep(1200);
                    mSoundPool.play(soundPoolMap.get(KEY_SOUND_A04), 1, 1, 0, 0, 1);
                }
                break;
            case R.id.button7:
                iSound = mCard.finger[5 + 512];

                mSoundPool.play(soundPoolMap.get(iSound), 1, 1, 0, 0, 1);

                if (!mClient.openFinger()) {
                    TextView v1 = (TextView) findViewById(R.id.textView11);
                    v1.setText("指纹采集器异常");
                    mClient.closeFinger();
                    mSoundPool.play(soundPoolMap.get(KEY_SOUND_A05), 1, 1, 0, 0, 1);
                    break;
                }

                SystemClock.sleep(1000);

                if (!mClient.collectFinger()) {
                    TextView v1 = (TextView) findViewById(R.id.textView11);
                    v1.setText("指纹采集失败");
                    mClient.closeFinger();
                    mSoundPool.play(soundPoolMap.get(KEY_SOUND_A05), 1, 1, 0, 0, 1);
                    break;
                }

                SystemClock.sleep(1000);
                mSoundPool.play(soundPoolMap.get(KEY_SOUND_A06), 1, 1, 0, 0, 1);

                finger[0] = mCard.finger[5 + 512];
                if (!mClient.downFinger(null)) {
                    TextView v1 = (TextView) findViewById(R.id.textView11);
                    v1.setText("指纹下载失败");
                    mClient.closeFinger();
                    SystemClock.sleep(1200);
                    mSoundPool.play(soundPoolMap.get(KEY_SOUND_A05), 1, 1, 0, 0, 1);
                    break;
                }

                //SystemClock.sleep(3000);
                if (!mClient.matchFinger()) {
                    TextView v1 = (TextView) findViewById(R.id.textView11);
                    v1.setText("指纹比对失败");
                    mClient.closeFinger();
                    SystemClock.sleep(1200);
                    mSoundPool.play(soundPoolMap.get(KEY_SOUND_A05), 1, 1, 0, 0, 1);
                } else {
                    TextView v1 = (TextView) findViewById(R.id.textView11);
                    v1.setText("指纹比对成功");
                    mClient.closeFinger();
                    SystemClock.sleep(1200);
                    mSoundPool.play(soundPoolMap.get(KEY_SOUND_A04), 1, 1, 0, 0, 1);
                }
                break;
        }
    }

    private void switchBtn(int btn[], boolean open) {
        if (open) {
            for (int i = 0; i < id.length; i++) {
                Button b = (Button) findViewById(id[i]);
                b.setEnabled(false);
            }
            for (int i = 0; i < btn.length; i++) {
                Button b = (Button) findViewById(btn[i]);
                b.setEnabled(true);
            }
        } else {
            for (int i = 0; i < id.length; i++) {
                Button b = (Button) findViewById(id[i]);
                b.setEnabled(true);
            }
            for (int i = 0; i < btn.length; i++) {
                Button b = (Button) findViewById(btn[i]);
                b.setEnabled(false);
            }
        }
    }

    public void onBtState(final boolean is_connect) {
        //Toast.makeText(this, "connect state is " + is_connect, 2000).show();
        if (is_connect) {
            switchBtn(new int[]{R.id.button1, R.id.button4}, false);
            initView("连接成功");
        } else {
            switchBtn(new int[]{R.id.button1, R.id.button4}, true);
            initView("连接关闭");
        }
    }


    void initView(String szTxt) {
        TextView v = (TextView) findViewById(R.id.textView1);
        //setTitle();
        v.setText("姓名 ");
        v = (TextView) findViewById(R.id.textView2);
        v.setText("性别  ");
        v = (TextView) findViewById(R.id.textView3);
        v.setText("民族  ");
        v = (TextView) findViewById(R.id.textView4);
        v.setText("出生 ");
        v = (TextView) findViewById(R.id.textView5);
        v.setText("住址 ");
        v = (TextView) findViewById(R.id.textView6);
        v.setText("身份证号");
        v = (TextView) findViewById(R.id.textView7);
        v.setText("签发机关 ");
        v = (TextView) findViewById(R.id.textView8);
        v.setText("有效期限  ");
        v = (TextView) findViewById(R.id.textView9);
        v.setText(new String(" "));
        v = (TextView) findViewById(R.id.textView10);
        v.setText(new String(" "));
        v = (TextView) findViewById(R.id.textView11);
        v.setText(new String(szTxt));

        v = (TextView) findViewById(R.id.textView12);
        v.setText(new String(""));

        ImageView mImageView = (ImageView) findViewById(R.id.imageView1);
        mImageView.setImageDrawable(getResources().getDrawable(R.drawable.tmp));
    }

    void initBtn() {
        Button b;
        for (int i = 0; i < id.length; i++) {
            b = (Button) findViewById(id[i]);
            b.setOnClickListener(this);
        }
        switchBtn(new int[]{R.id.button1, R.id.button4, R.id.button5}, true);

        b = (Button) findViewById(R.id.button5);
        b.setOnClickListener(this);
    }

}
